package io.terminus.spzx.pay.service.impl;

import com.alibaba.fastjson.JSON;
import io.terminus.spzx.feign.order.OrderFeignClient;
import io.terminus.spzx.feign.product.ProductFeignClient;
import io.terminus.spzx.model.dto.product.SkuSaleDto;
import io.terminus.spzx.model.entity.order.OrderInfo;
import io.terminus.spzx.model.entity.order.OrderItem;
import io.terminus.spzx.model.entity.pay.PaymentInfo;
import io.terminus.spzx.pay.mapper.PaymentMapper;
import io.terminus.spzx.pay.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 【移动端】支付管理Service实现类
 * @Date 2024-12-29
 */
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl implements PaymentService {

    private final OrderFeignClient orderFeignClient;

    private final ProductFeignClient productFeignClient;

    private final PaymentMapper paymentMapper;

    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        PaymentInfo paymentInfo = paymentMapper.getByOrderNo(orderNo);
        if (Objects.isNull(paymentInfo)) {
            OrderInfo orderInfo = orderFeignClient.getOrderInfoByOrderNo(orderNo);
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPayType(orderInfo.getPayType());
            String content = StringUtils.EMPTY;
            for (OrderItem item : orderInfo.getOrderItemList()) {
                content += item.getSkuName() + " ";
            }
            paymentInfo.setContent(content);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPaymentStatus(0);
            paymentMapper.save(paymentInfo);
        }

        return paymentInfo;
    }

    @Override
    public void updatePaymentStatus(Map<String, String> paramMap) {
        // 查询PaymentInfo
        PaymentInfo paymentInfo = paymentMapper.getByOrderNo(paramMap.get("out_trade_no"));
        if (paymentInfo.getPaymentStatus() == 1) {
            return;
        }

        // 更新支付信息
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOutTradeNo(paramMap.get("trade_no"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(paramMap));
        paymentMapper.updateById(paymentInfo);

        // 更新订单的支付状态: 待发货
        orderFeignClient.updateOrderStatus(paymentInfo.getOrderNo() , 1) ;

        // 更新商品销量
        OrderInfo orderInfo = orderFeignClient.getOrderInfoByOrderNo(paymentInfo.getOrderNo());
        List<SkuSaleDto> skuSaleDtoList = orderInfo.getOrderItemList().stream().map(item -> {
            SkuSaleDto skuSaleDto = new SkuSaleDto();
            skuSaleDto.setSkuId(item.getSkuId());
            skuSaleDto.setNum(item.getSkuNum());
            return skuSaleDto;
        }).collect(Collectors.toList());
        productFeignClient.updateSkuSaleNum(skuSaleDtoList) ;
    }
}
